package com.bup.gn.utils;

import com.bup.gn.model.User;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

import javax.inject.Inject;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Locale;

@Service(value = "emailEngine")
public class EmailEngine {
    @Inject
    private TemplateEngine templateEngine;

    @Inject
    private JavaMailSender mailSender;

    public void sendRegisterEmail(HttpServletRequest request, User account) {
        try {
            final WebContext webContext = new WebContext(request,
                    request.getSession().getServletContext(), Locale.ENGLISH);
            webContext.setVariable("register_date", new Date());
            String link = AppUtils.getPrefixUrl(request)
                    + "process-register-validation.html?security_key=" + account.getSecurityKey();
            webContext.setVariable("link", link);
            String htmlContent = templateEngine.process("email-register.html", webContext);

            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
            message.setFrom("support@gachnoi.net");
            message.setTo(account.getEmail());
            message.setSubject("Congratulation! You registered successfully.");
            message.setText(htmlContent, true);
            this.mailSender.send(mimeMessage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void sendConfirmRegisterValidation(HttpServletRequest request, User account) {
        try {
            final WebContext webContext = new WebContext(request,
                    request.getSession().getServletContext(), Locale.ENGLISH);
            webContext.setVariable("register_date", new Date());
            String htmlContent = templateEngine.process("email-register-confirm.html", webContext);

            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
            message.setFrom("support@gachnoi.net");
            message.setTo(account.getEmail());
            message.setSubject("Congratulation! Confirm successfully.");
            message.setText(htmlContent, true);
            this.mailSender.send(mimeMessage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void sendResetEmail(HttpServletRequest request, User account) {
        try {
            final WebContext webContext = new WebContext(request,
                    request.getSession().getServletContext(), Locale.ENGLISH);
            webContext.setVariable("reset_date", new Date());
            String link = AppUtils.getPrefixUrl(request)
                    + "show-password-input.html?security_key=" + account.getSecurityKey();
            webContext.setVariable("link", link);
            String htmlContent = templateEngine.process("email-reset.html", webContext);

            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
            message.setFrom("support@gachnoi.net");
            message.setTo(account.getEmail());
            message.setSubject("Reset password.");
            message.setText(htmlContent, true);
            this.mailSender.send(mimeMessage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
